//
//  GAOVisitorView.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class GAOVisitorView: UIView {
    
    var visitorViewClosure: (()->())?
    
    //  MARK:懒加载控件
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文本信息
    private lazy var  messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    
    //  注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return button
    }()
    
    //  登陆按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登陆", forState: .Normal)
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //给子控件添加约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 40))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
    }
    
    
    @objc private func registerButtonAction() {
        visitorViewClosure?()
    }
    
    @objc private func loginButtonAction() {
        visitorViewClosure?()
    }
    
    //开始动画
    private func startAnimation() {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotationAnimation.toValue = 2 * M_PI
        rotationAnimation.duration = 5.0
        rotationAnimation.repeatCount = MAXFLOAT
        //防止动画被释放掉
        rotationAnimation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(rotationAnimation, forKey: nil)
    }
    
    func setVistorInfo(message: String?, imageName: String?) {
        //判断 message和imageName无值进入首页界面,有值进入其它界面
        
        if let InfoMessage = message, InfoImageName = imageName {
            messageLabel.text = InfoMessage
            iconImageView.image = UIImage(named: InfoImageName)
            cycleImageView.hidden = true
        }else {
            cycleImageView.hidden = false
            startAnimation()
        }
    }
    
}
